Imports Microsoft.VisualBasic
Imports System.Globalization

Public Class ItemMasterData
    Public Function Item_Add(ByVal ItemName As String, ByVal FrName As String, ByVal ItemGroup As String, ByVal Puchase As String, ByVal Sales As String, ByVal Inv As String, ByVal UserID As String) As String
        Try
            Dim return_value As Integer = 0
            Dim SerrorMsg As String = ""
            Dim oITM As SAPbobsCOM.Items
            Dim oITB As SAPbobsCOM.ItemGroups
            oITM = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)
            oITB = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItemGroups)
            Dim oRecordSetBP As SAPbobsCOM.Recordset
            oRecordSetBP = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSetBP.DoQuery("SELECT T0.[ItmsGrpCod] FROM OITB T0 WHERE T0.[ItmsGrpNam] ='" & ItemGroup & "'")
            If oRecordSetBP.RecordCount = 0 Then
                oITB.GroupName = ItemGroup
                return_value = oITB.Add()
                PublicVariable.oCompany.GetLastError(return_value, SerrorMsg)
                If return_value <> 0 Then
                    Return SerrorMsg
                End If
            End If
            oITM.Series = PublicVariable.ItemSeries
            oITM.ItemName = ItemName
            oITM.ItemsGroupCode = ItemGroupCode(ItemGroup)
            If FrName = "Yes" Then
                oITM.ManageBatchNumbers = SAPbobsCOM.BoYesNoEnum.tYES
            End If
            If Puchase = "Y" Then
                oITM.PurchaseItem = SAPbobsCOM.BoYesNoEnum.tYES
            Else
                oITM.PurchaseItem = SAPbobsCOM.BoYesNoEnum.tNO
            End If
            If Sales = "Y" Then
                oITM.SalesItem = SAPbobsCOM.BoYesNoEnum.tYES
            Else
                oITM.SalesItem = SAPbobsCOM.BoYesNoEnum.tNO
            End If
            If Inv = "Y" Then
                oITM.InventoryItem = SAPbobsCOM.BoYesNoEnum.tYES
            Else
                oITM.InventoryItem = SAPbobsCOM.BoYesNoEnum.tNO
            End If
            oRecordSetBP.DoQuery("SELECT T0.[Name], T0.[U_Country],GetDate() Dt FROM [dbo].[@USER]  T0 WHERE T0.[Code]  ='" & UserID & "'")
            'Dim d As String = Now.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) 'CDate(oRecordSetBP.Fields.Item("Dt").Value).ToString("dd/MM/yyyy") 'Format(oRecordSetBP.Fields.Item("Dt").Value, "dd/MM/yyyy").ToString
            'Dim d1 As Date = DateFunction(Format(Now.Date, "dd/MM/yy"))
            'Dim [date] As DateTime = DateTime.Today
            'oITM.UserFields.Fields.Item("U_AB_CreatedDate").Value = [date].ToString("yyyyMMdd", CultureInfo.InvariantCulture)

            oITM.UserFields.Fields.Item("U_AB_CreatedBy").Value = oRecordSetBP.Fields.Item("Name").Value 'Format(Now.Date, "dd/MM/yyyy")
            oITM.UserFields.Fields.Item("U_AB_WWCompany").Value = oRecordSetBP.Fields.Item("U_Country").Value 'Format(Now.Date, "dd/MM/yyyy")d1.Date 'Now.Date.ToString("", CultureInfo.InvariantCulture) 'CDate(oRecordSetBP.Fields.Item("Dt").Value).ToString("dd/MM/yyyy") 'oRecordSetBP.Fields.Item("Dt").Value

            return_value = oITM.Add()
            PublicVariable.oCompany.GetLastError(return_value, SerrorMsg)
            If return_value = 0 Then
                Return "Item Created Successfully!"
            Else
                Return SerrorMsg
            End If
        Catch ex As Exception
            Return ex.Message
        End Try
        Return ""
    End Function
    Public Function Item_Update(ByVal ItemCode As String, ByVal ItemName As String, ByVal FrName As String, ByVal ItemGroup As String, ByVal UserID As String) As String
        Try
            Dim return_value As Integer = 0
            Dim SerrorMsg As String = ""
            Dim oITM As SAPbobsCOM.Items
            Dim oITB As SAPbobsCOM.ItemGroups
            oITM = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)
            oITB = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItemGroups)
            Dim oRecordSetBP As SAPbobsCOM.Recordset
            If oITM.GetByKey(ItemCode) = True Then
                oRecordSetBP = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                'oRecordSetBP.DoQuery("SELECT T0.[ItmsGrpCod] FROM OITB T0 WHERE T0.[ItmsGrpNam] ='" & ItemGroup & "'")
                'If oRecordSetBP.RecordCount = 0 Then
                '    oITB.GroupName = ItemGroup
                '    return_value = oITB.Add()
                '    PublicVariable.oCompany.GetLastError(return_value, SerrorMsg)
                '    If return_value <> 0 Then
                '        Return SerrorMsg
                '    End If
                'End If

                oITM.ItemName = ItemName
                'oITM.ItemsGroupCode = ItemGroupCode(ItemGroup)
                'oITM.ForeignName = FrName
                'If Puchase = "Y" Then
                '    oITM.PurchaseItem = SAPbobsCOM.BoYesNoEnum.tYES
                'Else
                '    oITM.PurchaseItem = SAPbobsCOM.BoYesNoEnum.tNO
                'End If
                'If Sales = "Y" Then
                '    oITM.SalesItem = SAPbobsCOM.BoYesNoEnum.tYES
                'Else
                '    oITM.SalesItem = SAPbobsCOM.BoYesNoEnum.tNO
                'End If
                'If Inv = "Y" Then
                '    oITM.InventoryItem = SAPbobsCOM.BoYesNoEnum.tYES
                'Else
                '    oITM.InventoryItem = SAPbobsCOM.BoYesNoEnum.tNO
                'End If
                oRecordSetBP.DoQuery("SELECT T0.[Name], T0.[U_Country],GetDate() Dt FROM [dbo].[@USER]  T0 WHERE T0.[Code]  ='" & UserID & "'")
                'Dim d1 As Date = DateFunction(Format(Now.Date, "dd/MM/yy"))
                'Dim [date] As DateTime = DateTime.Today
                'oITM.UserFields.Fields.Item("U_AB_UpdateDate").Value = [Date].ToString("yyyyMMdd", CultureInfo.InvariantCulture)

                oITM.UserFields.Fields.Item("U_AB_UpdatedBy").Value = oRecordSetBP.Fields.Item("Name").Value 'Format(Now.Date, "dd/MM/yyyy")
                'oITM.UserFields.Fields.Item("U_AB_WWCompany").Value = oRecordSetBP.Fields.Item("U_Country").Value; 'd1.Date 'Now.Date.ToString("", CultureInfo.InvariantCulture) 'Format(oRecordSetBP.Fields.Item("Dt").Value, "dd/MM/yyyy")

                return_value = oITM.Update()
                PublicVariable.oCompany.GetLastError(return_value, SerrorMsg)
                If return_value = 0 Then
                    Return "Item Updated Successfully!"
                Else
                    Return SerrorMsg
                End If
            Else
                Return "Item Code Not Found!"
            End If
        Catch ex As Exception
            Return ex.Message
        End Try
        Return ""
    End Function
    Public Function ItemGroupCode(ByVal ItemGroupName As String) As Integer
        Dim oRecordSetBP As SAPbobsCOM.Recordset
        oRecordSetBP = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSetBP.DoQuery("SELECT T0.[ItmsGrpCod] FROM OITB T0 WHERE T0.[ItmsGrpNam] ='" & ItemGroupName & "'")
        If oRecordSetBP.RecordCount > 0 Then
            Return oRecordSetBP.Fields.Item(0).Value
        Else
            Return 100
        End If

    End Function
    Function DateFunction(ByVal dt As String) As Date
        Dim d, m, y As String
        Dim Z As Date
        d = dt.Substring(0, 2)
        m = dt.Substring(3, 2)
        y = dt.Substring(6, 2)
        'Z = CDate("May 25, 1986")
        Z = CDate(d & "/" & m & "/" & y)
        'Return System.DateTime.Parse(dt, format1, Globalization.DateTimeStyles.None)
        Return Z
    End Function
End Class
